<?php

class myUser extends sfGuardSecurityUser
{
	private $player;
        private $activ_player;
	private $players=array(1,2);
	private $iMatchId=1;
        private $oGame;
        private $oTournament;
	
	public function initialize(sfEventDispatcher $dispatcher, sfStorage $storage, $options = array())
	{
		parent::initialize($dispatcher, $storage, $options);
		$this->setAttribute('user_id',0);
		$this->setAttribute('user_admin',0);
		$this->setAttribute('user_mod',0);
                $this->setAttribute('user_activ_id',0);
		$this->setAttribute('is_ingame',0);
	}
	
                
	public function getTournament()
        {
            if($this->isAuthenticated())
            {
                if($this->oTournament == null)
                {
                    $this->oTournament=  $this->getActivPlayer()->getTournament();
                }
                return $this->oTournament;
            }
        }
        
        function isFirstGamePlayer($oRule,$iMatchCount,$iGameCount,$bIsFirstPlayer)
        {
           /* if ($bIsFirstPlayer) {
               return $iGameCount%2;
            } else {
               return  !($iGameCount%2);
            }*/
            return $bIsFirstPlayer;
        }
       	
	function getPlayer()
	{
		if($this->isAuthenticated())
		{
			if($this->player == null)
			{
				$criteria = new Criteria ();
				$criteria -> add (PlayerPeer::NICK,$this->getUsername(), Criteria::EQUAL );				
				$this->pracownik=PlayerPeer::doSelectOne($criteria);
				$this->setAttribute('user_id',$this->pracownik->getId());
				$this->setAttribute('user_admin',$this->pracownik->getAdmin());
				$this->setAttribute('user_mod',$this->pracownik->getMod());
				$this->players[0]=$this->pracownik->getId();
				
			}
			return $this->pracownik;
		}
		
		return null;
	}
        
        function getActivPlayer()
	{
		return ActivPlayerPeer::retrieveByUserAndStatuNJoinAll($this->getUserId(),  ActivPlayer::$ACTION_END_TURNAMENT);
	}
        
        
        function getActivPlayerId()
        {
            $player=$this->getActivPlayer();
            if($player)
            {
                if($player->getStatus()!=ActivPlayer::$ACTION_END_TURNAMENT)
                {
                    return $player->getId();
                }else{
                    $this->activ_player = null;
                }
            }
            return 0;
        }


        function getUserId()
	{		
		$this->getPlayer();
		return $this->getAttribute('user_id');
	}
	
	function isAdmin()
	{		
		$this->getPlayer();
		return $this->getAttribute('user_admin');
	}
	
	function isMod()
	{		
		$this->getPlayer();
		return $this->getAttribute('user_mod');
	}
	
	function isPremium()
	{		
		$this->getPlayer();
		return $this->getAttribute('user_mod') || $this->getAttribute('user_admin');
	}
	
}
